﻿// CMynew1.cpp: 实现文件
//

#include "stdafx.h"
#include "Item.h"
#include "CMynew1.h"
#include "afxdialogex.h"
#include "VIDEO.h"//导入包
VIDEO video;//创建对象
Video video1;//创建对象


const unsigned char FORE_GROUD = 127;
int thresh = 10;

// CMynew1 对话框

IMPLEMENT_DYNAMIC(CMynew1, CDialogEx)

CMynew1::CMynew1(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG2, pParent)
{

}

CMynew1::~CMynew1()
{
}

void CMynew1::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SCROLLBAR1, Scroll);

	namedWindow("view", WINDOW_AUTOSIZE);
	HWND hWnd = (HWND)cvGetWindowHandle("view");
	HWND hParent = ::GetParent(hWnd);
	::SetParent(hWnd, GetDlgItem(IDC_DOC1)->m_hWnd);
	::ShowWindow(hParent, SW_HIDE);

	namedWindow("view1", WINDOW_AUTOSIZE);
	HWND hWnd1 = (HWND)cvGetWindowHandle("view1");
	HWND hParent1 = ::GetParent(hWnd1);
	::SetParent(hWnd1, GetDlgItem(IDC_DOC2)->m_hWnd);
	::ShowWindow(hParent1, SW_HIDE);

	namedWindow("view2", WINDOW_AUTOSIZE);
	HWND hWnd2 = (HWND)cvGetWindowHandle("view2");
	HWND hParent2 = ::GetParent(hWnd2);
	::SetParent(hWnd2, GetDlgItem(IDC_DOC3)->m_hWnd);
	::ShowWindow(hParent2, SW_HIDE);
	
}


BEGIN_MESSAGE_MAP(CMynew1, CDialogEx)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BUTTON4, &CMynew1::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON1, &CMynew1::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_KUAITUI, &CMynew1::OnBnClickedKuaitui)
	ON_BN_CLICKED(IDC_KUAIJIN, &CMynew1::OnBnClickedKuaijin)
	ON_BN_CLICKED(IDC_BUTTON2, &CMynew1::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CMynew1::OnBnClickedButton3)
END_MESSAGE_MAP()


// CMynew1 消息处理程序


BOOL CMynew1::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

//滚动条
void CMynew1::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	video.SCroll(nSBCode, nPos, pScrollBar);
	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}

//播放暂停

void CMynew1::OnBnClickedButton4()
{
	video.ZanTing();
	// TODO: 在此添加控件通知处理程序代码
}


// 读取视频
void CMynew1::OnBnClickedButton1()
{
	CRect rect;
	GetDlgItem(IDC_DOC1)->GetClientRect(&rect);
	video.VideoShow(Scroll, rect, "view");

	
	
	
}


void CMynew1::OnBnClickedKuaitui()
{
	// TODO: 在此添加控件通知处理程序代码

	video.KuaiTui(Scroll);
}


void CMynew1::OnBnClickedKuaijin()
{
	// TODO: 在此添加控件通知处理程序代码
	video.KuaiJin(Scroll);
}


void CMynew1::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	CRect rect;
	GetDlgItem(IDC_DOC2)->GetClientRect(&rect);
	//video1.VideoBeijing(picpath, "view1");
}


void CMynew1::OnBnClickedButton3()
{
	
	VideoCapture video("F:\\photo\\car.avi");

	//判断如果video是否可以打开
	/*if (!video.isOpened())
		return;*/

	//用于保存当前帧的图片
	Mat currentBGRFrame;

	//用来保存上一帧和当前帧的灰度图片
	Mat previousGrayFrame;
	Mat currentGaryFrame;

	//用来保存帧差
	Mat frameDifference;//CV_16SC1

	//用来保存帧差的绝对值
	Mat absFrameDifferece;

	//用来显示前景
	Mat segmentation;

	//显示原视频
	//namedWindow("原视频", 0);

	//显示前景
	//namedWindow("二值化", 0);
	cvResizeWindow("二值化", 500, 500);
	createTrackbar("阈值:", "二值化", &thresh, FORE_GROUD, NULL);

	//帧数
	int numberFrame = 0;

	//形态学处理用到的算子
	Mat morphologyKernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));

	for (;;)
	{
		//读取当前帧
		video >> currentBGRFrame;


		//判断当前帧是否存在
		if (!currentBGRFrame.data)
			break;


		numberFrame++;


		if (numberFrame == 1)
		{
			//颜色空间的转换
			cvtColor(currentBGRFrame, currentGaryFrame, COLOR_BGR2GRAY);
			//保存当前帧的灰度图
			previousGrayFrame = currentGaryFrame.clone();
			//imshow("原视频", currentBGRFrame);
			imshow("view1", currentBGRFrame);
			continue;
		}
		else
		{
			//颜色空间的转换
			cvtColor(currentBGRFrame, currentGaryFrame, COLOR_BGR2GRAY);


			//src1-src2
			subtract(currentGaryFrame, previousGrayFrame, frameDifference, Mat(), CV_16SC1);


			//取绝对值
			absFrameDifferece = abs(frameDifference);


			//位深的改变
			absFrameDifferece.convertTo(absFrameDifferece, CV_8UC1, 1, 0);


			//阈值处理
			threshold(absFrameDifferece, segmentation, double(thresh), double(FORE_GROUD), THRESH_BINARY);


			//中值滤波
			medianBlur(segmentation, segmentation, 3);


			//形态学处理(开闭运算)
			morphologyEx(segmentation, segmentation, MORPH_OPEN, morphologyKernel, Point(-1, -1), 1, BORDER_REPLICATE);
			morphologyEx(segmentation, segmentation, MORPH_CLOSE, morphologyKernel, Point(-1, -1), 2, BORDER_REPLICATE);


			//显示二值化图片
			//imshow("二值化", segmentation);


			//找边界
			vector< vector<Point> > contours;	//contours定义为2维浮点向量
			vector<Vec4i> hierarchy;	//定义的层级
			findContours(segmentation, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); //CV_RETR_TREE
			vector< vector<Point> > contours_poly(contours.size());


			/*存储运动物体*/
			vector<Rect> boundRect;
			boundRect.clear();


			//画出运动物体
			//对视频中出现的运动物体，进行初次的筛选
			for (int index = 0; index < contours.size(); index++)
			{
				approxPolyDP(Mat(contours[index]), contours_poly[index], 3, true);
				Rect rect = boundingRect(Mat(contours_poly[index]));
				if (rect.area() > 400)
					rectangle(currentBGRFrame, rect, Scalar(0, 255, 255), 2);
			}


			//显示原视频
			//namedWindow("原视频", 1);
			//imshow("原视频", currentBGRFrame);
			imshow("view1", currentBGRFrame);


			//保存当前帧的灰度图
			previousGrayFrame = currentGaryFrame.clone();
		}


		if (waitKey(33) == 'q')
			break;


	}
}
